package com.asktgapp.base;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.asktgapp.dialog.CProgressDialogUtils;
import com.asktgapp.dialog.TokenInvalidDialog;
import com.asktgapp.hxsg.BiuEaseHelper;
import com.asktgapp.modulebase.R;
import com.asktgapp.modulebase.common.util.LogUtil;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.modulebase.common.util.Util;
import com.asktgapp.user.activity.EngineerDetailActivity;
import com.asktgapp.user.activity.LoginActivity;
import com.asktgapp.user.activity.PhotoViewActivity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;


/**
 * @author Lee
 * @Title: {Fragment 基类}
 * @Description:{描述}
 * @date 2016/1/25
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener {

    private static final String TAG = "BaseFragment";
    protected BaseFragmentActivity mBaseFragmetActivity;
    protected BaseActivity mBaseActivity;

    private ViewGroup mContainert;
    private ViewGroup loading_layout;
    private ViewGroup no_data_layout;
    private FrameLayout no_net_layout;

    private ViewStub mLoadingVS, mNoNetVS, mNoDataVS;
    private Button noNetWorkBtn;
    protected CustomToolBrBaseActivity mActivity;
    private AnimationDrawable frameAnimation;
    Toast mToast;
    private boolean isLoadingInflate = false, isNoDataInflate = false, isNoNetInflate = false;
    private HashSet<retrofit2.Call> mapCall;

    public void addSubscription(retrofit2.Call s) {
        if (this.mapCall == null) {
            this.mapCall = new HashSet<>();
        }
        this.mapCall.add(s);
    }

    public void removeSubscription(retrofit2.Call s) {
        if (this.mapCall != null && mapCall.size() > 0) {
            this.mapCall.remove(s);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        dismissProgress();
        if (this.mapCall != null && mapCall.size() > 0) {
            Iterator it = mapCall.iterator();

            while (it.hasNext()) {
                retrofit2.Call call = (retrofit2.Call) it.next();
                call.cancel();
            }
        }

    }

    /**
     * 设置OptionMenu
     */
    protected void setOptionMenu(boolean hasOption, int menuId) {
    }

    /**
     * 初始化控件
     */
    protected abstract void initView(View rootView);

    /**
     * 加载数据，一切网络请求方法在此方法中写
     */
    public abstract void loadData();

    /**
     * 获取intent中的extra
     */
    protected void getIntentData() {
    }

    /**
     * 设置监听事件
     */
    protected void setListener() {
    }

    /**
     * 重新加载数据(子 Fragment 加载数据失败可覆盖该方法通过点击 NoDataLayout 任意位置进行重新加载数据)
     */
    protected void reLoadData() {
    }

    protected ViewGroup getContainer(View rootView) {
        return null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        ViewGroup cont = getContainer(container);
        View rootView = inflater.inflate(R.layout.fragment_base_frame_load, cont != null ? cont : container,
                true);
        mNoDataVS = rootView.findViewById(R.id.vs_non);
        mNoNetVS = rootView.findViewById(R.id.vs_no_net);
        mLoadingVS = rootView.findViewById(R.id.vs_loading);
        mNoDataVS.setOnInflateListener(new ViewStub.OnInflateListener() {
            @Override
            public void onInflate(ViewStub stub, View inflated) {
                isNoDataInflate = true;
            }
        });
        mNoNetVS.setOnInflateListener(new ViewStub.OnInflateListener() {
            @Override
            public void onInflate(ViewStub stub, View inflated) {
                isNoNetInflate = true;
            }
        });
        mLoadingVS.setOnInflateListener(new ViewStub.OnInflateListener() {
            @Override
            public void onInflate(ViewStub stub, View inflated) {
                isLoadingInflate = true;
            }
        });
//        loading_layout = (ViewGroup) rootView.findViewById(R.id.loading_layout);
//        no_data_layout = (ViewGroup) rootView.findViewById(R.id.no_data_layout);
//        no_net_layout = (FrameLayout) rootView.findViewById(R.id.no_net_layout);
//        noNetWorkBtn = (Button) rootView.findViewById(R.id.tryBtn);
//        noNetWorkBtn.setOnClickListener(this);
//        no_data_layout.setOnClickListener(this);
        ProgressBar loadingImg = (ProgressBar) rootView.findViewById(R.id.loading);
//        if (loadingImg != null) {
//            frameAnimation = (AnimationDrawable) loadingImg.getDrawable();
//        }
        return cont != null ? container : rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Activity activity = getActivity();
        if (activity instanceof BaseFragmentActivity) {
            mBaseFragmetActivity = (BaseFragmentActivity) activity;
        }
        if (activity instanceof CustomToolBrBaseActivity) {
            mActivity = (CustomToolBrBaseActivity) activity;
        }
        if (activity instanceof BaseActivity) {
            mBaseActivity = (BaseActivity) activity;
        }
        getIntentData();
        initView(getView());
        setListener();
        loadData();
    }


    /**
     * 跳轉图片预览界面的Intent
     *
     * @param position 点击图片在图片集中的位置
     * @param imgList  图片集
     */
    public void seeBigListPic(int position, ArrayList<String> imgList) {
        Intent intent = new Intent(getActivity(), PhotoViewActivity.class);
        intent.putExtra("position", position);
        intent.putStringArrayListExtra("imgs", imgList);
        startActivity(intent);
    }

    public void seeBigPic(int position, String url) {
        ArrayList list = new ArrayList<>();
        list.add(url);
        Intent intent = new Intent(getActivity(), PhotoViewActivity.class);
        intent.putExtra("position", position);
        intent.putExtra("type", 1);
        intent.putStringArrayListExtra("imgs", list);
        startActivity(intent);
    }

///**
// * 跳转他人的个人中心
// * 传入id
// * **/
//   public  void startOtherInfoIntent(String id){
//        Intent intent=new Intent(getActivity(), OthersInfoActvity.class);
//       intent.putExtra("id",id);
//       startActivity(intent);
//   }

    /**
     * Toast
     *
     * @param msg
     * @param time LENGTH_SHORT = 0;/ LENGTH_LONG = 1;
     */
    public void showTost(String msg, int time) {
        if (getActivity() != null) {
            if (msg.equals("未知错误")) {
                msg = "网络繁忙";
            }
            if (mToast == null) {
                if (time == 0) {
                    mToast = Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT);
                } else mToast = Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG);

            } else {
                mToast.setText(msg);
                if (time == 0) {
                    mToast.setDuration(Toast.LENGTH_SHORT);
                } else mToast.setDuration(Toast.LENGTH_LONG);
            }
            mToast.show();

        }
    }


    /**
     * 显示loading
     * 初始化界面时首先显示的元素
     */
    public void visibleLoading() {
        if (!isLoadingInflate) {
            mLoadingVS.inflate();
            loading_layout = getView().findViewById(R.id.loading_layout);
        }
        inVisibleNoData();
        inVisibleNoNetWork();
        if (!loading_layout.isShown()) {
            LogUtil.LogE(TAG, "visibleLoading");
            loading_layout.setVisibility(View.VISIBLE);
            // Start the animation (looped playback by default).
            if (frameAnimation != null) {
                frameAnimation.start();
            }
        }

    }

    /**
     * 隐藏loading
     */
    public void inVisibleLoading() {
        LogUtil.LogE(TAG, "inVisibleLoading");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (frameAnimation != null && frameAnimation.isRunning()) {
                    frameAnimation.stop();
                }
                if (loading_layout != null)
                    loading_layout.setVisibility(View.GONE);
            }
        }, 800);

    }

    /**
     * 显示空数据布局
     */
    public void visibleNoData() {
        if (getBaseActivity().isFinishing())
            return;
        if (!isNoDataInflate) {
            mNoDataVS.inflate();
            no_data_layout = getView().findViewById(R.id.no_data_layout);
        }
        inVisibleLoading();
        inVisibleNoNetWork();
        if (!no_data_layout.isShown()) {
            if (no_data_layout != null)
                no_data_layout.setVisibility(View.VISIBLE);
        }
    }


    public void visibleNoNetWork() {
        if (!isNoNetInflate) {
            mNoNetVS.inflate();
            no_net_layout = getView().findViewById(R.id.no_data_layout);
        }
        inVisibleLoading();
        inVisibleNoData();
        if (no_net_layout != null)
            no_net_layout.setVisibility(View.VISIBLE);
    }

    public void inVisibleNoNetWork() {
        if (no_net_layout != null)
            no_net_layout.setVisibility(View.GONE);
    }

    /**
     * 影藏空数据布局
     */
    public void inVisibleNoData() {
        if (no_data_layout != null)
            no_data_layout.setVisibility(View.GONE);
    }

    /**
     * 显示progress loading
     * 进行操作需请求网络时显示  如 登录 注册...
     *
     * @param requestTag 网络请求tag，用于取消请求
     */
    public void showProgress(String requestTag) {
//        LoadingDialogFragment loading = LoadingDialogFragment.newInstance(DialogFragment.STYLE_NO_TITLE, R.layout.loading, requestTag);
//        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
//        ft.addToBackStack(null);
//        loading.show(ft, "loading");
        CProgressDialogUtils.showProgressDialog(getActivity());
    }

//        FragmentManager fm = getChildFragmentManager();
//        LoadingDialogFragment loading = (LoadingDialogFragment) fm.findFragmentByTag("loading");
//        if (loading != null) {
//            loading.dismiss();
//        }

//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                FragmentManager fm = getChildFragmentManager();
//                LoadingDialogFragment loading = (LoadingDialogFragment) fm.findFragmentByTag("loading");
//                if (loading != null) {
//                    loading.dismiss();
//                }
//            }
//        },300);

//    }


    /**
     * 软盘控制
     * 显示或隐藏软键盘
     */
    public void showSoftKeyboard() {
        InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    public void showSoftKeyboard2(View focusedView) {
        InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(
                Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(focusedView, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 隐藏软键盘
     */
    public void hideSoftKeyboard() {
        InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context
                .INPUT_METHOD_SERVICE);
        if (inputManager.isActive() && getActivity().getCurrentFocus() != null) {
            inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
        }
    }


    /**
     * 隐藏progress loading
     */
    public void dismissProgress() {
//        FragmentManager fm = getChildFragmentManager();
//        final LoadingDialogFragment loading = (LoadingDialogFragment) fm.findFragmentByTag("loading");
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        }, 700);

        CProgressDialogUtils.cancelProgressDialog();
    }

    public boolean checkIsLogin(String mHintText) {
        if (Util.isEmpty(PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID))) {
            showUnLoginSnackbar(mHintText);
            return false;
        }
        return true;
    }

    public void showUnLoginSnackbar() {

        PreferencesUtil.putString(getActivity(), PreferencesUtil.USER_ID, "");
        PreferencesUtil.putString(getActivity(), PreferencesUtil.KEY_HX_ID, "");
        BiuEaseHelper.loginOut();
        TokenInvalidDialog dialog = new TokenInvalidDialog(null, new TokenInvalidDialog.onResultCallBack() {
            @Override
            public void onSure() {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });
        dialog.show(getFragmentManager(), "TokenInvalidDialog");

    }

    public void showUnLoginSnackbar(String mHintText) {

        PreferencesUtil.putString(getActivity(), PreferencesUtil.USER_ID, "");
        PreferencesUtil.putString(getActivity(), PreferencesUtil.KEY_HX_ID, "");
        BiuEaseHelper.loginOut();
        TokenInvalidDialog dialog = new TokenInvalidDialog(mHintText, new TokenInvalidDialog.onResultCallBack() {
            @Override
            public void onSure() {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });
        dialog.show(getFragmentManager(), "TokenInvalidDialog");

    }

    public void showSetNetworkSnackbar() {
        Activity mActivity = getActivity();
        if (mActivity != null && mActivity.getWindow() != null) {
            Snackbar snackbar = Snackbar.make(((ViewGroup) mActivity.getWindow().getDecorView().getRootView().findViewById(
                    android.R.id.content)).getChildAt(0), "当前无网络", Snackbar.LENGTH_SHORT).setAction("去设置",
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            setNetwork();
                        }
                    });
            snackbar.getView().setBackgroundColor(getResources().getColor(R.color.white));
            snackbar.setActionTextColor(getContext().getResources().getColor(R.color.colorAccent));
            snackbar.setDuration(Snackbar.LENGTH_LONG);
            TextView tv = (TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
            tv.setTextColor(getResources().getColor(R.color.colorAccent));
            snackbar.show();
        }

    }

    public void setNetwork() {
        Intent intent = null;
        /**
         * 判断手机系统的版本！如果API大于10 就是3.0+
         * 因为3.0以上的版本的设置和3.0以下的设置不一样，调用的方法不同
         */
        if (Build.VERSION.SDK_INT > 10) {
            intent = new Intent(Settings.ACTION_SETTINGS);
        } else {
            intent = new Intent();
            ComponentName component = new ComponentName(
                    "com.android.settings", "com.android.settings.Settings");
            intent.setComponent(component);
            intent.setAction("android.intent.action.VIEW");
        }
        startActivity(intent);
    }


    public void hideNavBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            View decorView = getActivity().getWindow().getDecorView();
            int uiOptions =
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
                uiOptions = uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            }
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    public void showNavBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            View decorView = getActivity().getWindow().getDecorView();
            int uiOptions =
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    public BaseFragmentActivity getBaseFragmentActivity() {
        if (mBaseFragmetActivity == null) {
            Activity activity = getActivity();
            if (activity instanceof BaseFragmentActivity) {
                mBaseFragmetActivity = (BaseFragmentActivity) activity;
            }
        }
        return mBaseFragmetActivity;
    }

    public BaseActivity getBaseActivity() {
        if (mBaseActivity == null) {
            Activity activity = getActivity();
            if (activity instanceof BaseActivity) {
                mBaseActivity = (BaseActivity) activity;
            }

            if (activity instanceof CustomToolBrBaseActivity) {
                mBaseActivity = (CustomToolBrBaseActivity) activity;
            }
        }
        return mBaseActivity;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.no_data_layout) {
            visibleLoading();
            loadData();
        } else if (v.getId() == R.id.tryBtn) {
            visibleLoading();
            loadData();
        }

    }

    public void SkipToEngineerDetail(Context context, String engineerID) {
        if (PreferencesUtil.getString(context, PreferencesUtil.USER_ID).equals(engineerID)) {
            showTost("本人的详情，请前往个人中心查看", 0);
            return;
        } else {
            Intent intent = new Intent(context, EngineerDetailActivity.class);
            intent.putExtra("id", engineerID);
            context.startActivity(intent);
        }
    }

}